package faf.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

import faf.business.SurveyManager;
import faf.entities.Account;
import faf.entities.Survey;

public class ListSurveyController implements Controller {

	SurveyManager surveyManager;

	public ModelAndView handleRequest(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		if (request.getParameter("surveydel") != null) {
			long id = Long.decode(request.getParameter("surveydel"));
			surveyManager.deleteSurvey(id);
		}

		Account account = (Account) request.getSession().getAttribute("user");
		if (account == null) {
			return new ModelAndView("login");
		}

		List<Survey> LS = surveyManager.findListSurvey(account.getId());

		Map<String, Object> surveyModel = new HashMap<String, Object>();
		surveyModel.put("sondages", LS);
		surveyModel.put("size", LS.size());
		surveyModel.put("account", account);

		return new ModelAndView("listesondage", "model", surveyModel);
	}

	public void setSurveyManager(SurveyManager surveyManager) {
		this.surveyManager = surveyManager;
	}

}
